﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Landscape.Business.FNH;
using Landscape.Business.FNH.Activities;
//using DCTM = Landscape.Business.Interface.Documentum;

namespace Landscape.WebApp.WCFServices
{
    [ServiceContract(Namespace = "Landscape")]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MaintenanceActions
    {        
        [OperationContract]
        public void SaveJob(Job pBusinessObject)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    var vslItem = nhSession.Get<Landscape.Business.Entities.VesselMachine>(pBusinessObject.VesselItemID);
                    vslItem.Jobs.Add(pBusinessObject.ToBusinessObject(nhSession));

                    MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                    mntActs.SaveEntity<Business.Entities.VesselMachine>(vslItem);
                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public List<Job> GetJobsByVesselItem(Guid pVesselItemCode, int pPageNo, int pPageSize)
        {
            List<Job> jobs = new List<Job>();
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                foreach (var item in mntActs.GetJobsByVesselItem(pVesselItemCode, pPageNo, pPageSize))
                {
                    jobs.Add(new Job(item));
                }
            }

            return jobs;
        }

        [OperationContract]
        public Job GetJobByID(Guid pJobCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                return new Job(mntActs.GetJobByCode(pJobCode));
            }
        }

        [OperationContract]
        public List<VesselMachine> GetVesselSystems(Guid pVesselCode, Guid pParentID)
        {
            List<VesselMachine> vesselItems = new List<VesselMachine>();

            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                foreach (var item in mntActs.GetVesselSystems(pVesselCode, pParentID))
                {
                    vesselItems.Add( new VesselMachine(item));
                }
            }

            return vesselItems;
        }

        [OperationContract]
        public void ReleaseJob(Guid pJobCode, Guid pUserCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                    var jobDet = mntActs.GetJobByCode(pJobCode);
                    jobDet.IsReleased = true;
                    jobDet.ReleasedDate = DateTime.Now;
                    jobDet.ReleasedBy = pUserCode;
                    jobDet.UpdatedBy = pUserCode;
                    jobDet.UpdatedDate = DateTime.Now;                    

                    mntActs.SaveEntity<Business.Entities.Job>(jobDet);

                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public void DeleteJob(Guid pJobCode, Guid pUserCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                    var jobDet = mntActs.GetJobByCode(pJobCode);
                    if (jobDet.IsReleased == true)
                    {//If released then soft delete
                        jobDet.IsDeleted = true;
                        jobDet.UpdatedBy = pUserCode;
                        jobDet.UpdatedDate = DateTime.Now;

                        mntActs.SaveEntity<Business.Entities.Job>(jobDet);
                    }
                    else
                    {//If not released then physical delete
                        nhSession.Delete(jobDet);
                    }

                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public void RemoveVesselItem(Guid pVesselItemCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                    var vslItem = mntActs.GetVesselItemByCode(pVesselItemCode);                    
                    nhSession.Delete(vslItem);

                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public void AddItem(Guid pItemCode, Guid pVesselCode, Guid pParentVesselItemCode, Guid pUserCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    MaintenanceActivities mntActs = new MaintenanceActivities(nhSession);
                    mntActs.AddMachineItemToVessel(pItemCode, pVesselCode, pParentVesselItemCode, pUserCode);

                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public List<DctmFile> GetFilesIn(string pFolderPath, string[] pFileNames)
        {
            List<DctmFile> files = new List<DctmFile>();

            //var sm = DCTM.SessionManager.GetManager();
            //var token = sm.CreateDCTMAdminSession();

            //var fldrID = DCTM.Actions.GetFolderDCTMID(pFolderPath, token);
            //if (fldrID.Trim() != string.Empty)
            //{
            //    DCTM.Folder fldr = new DCTM.Folder(token, fldrID);
            //    if (fldr.Documents.Count > 0)
            //    {
            //        int cnt = 0;
            //        var docs = ((pFileNames != null && pFileNames.Count() > 0) ? fldr.Documents.Where(exp => pFileNames.Where(exp2 => exp.ObjectName.ToLower().Contains(exp2.ToLower())).Count() > 0) : fldr.Documents);
            //        foreach (var f in docs)
            //        {
            //            cnt++;
            //            files.Add(new DctmFile()
            //            {
            //                DCTMID = f.DctmCode,
            //                FileName = f.ObjectName,
            //                SNo = cnt
            //            });
            //        }
            //    }
            //}

            return files;
        }        
    }

    [ServiceContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DctmFile
    {
        public int SNo
        { get; set; }

        public string DCTMID
        { get; set; }

        public string FileName
        { get; set; }
    }
}
